<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="24" :xxl="16">
            <a-form-item label="区域">
              <a-row :gutter="5" type="flex">
                <a-col :span="3">
                  <a-select
                    placeholder="省"
                    v-model="queryParam.provinceCode"
                    @change="e => addressChange(e, 'cityOptions', 'province')"
                  >
                    <a-select-option v-for="item in provinceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="市"
                    v-model="queryParam.cityCode"
                    @change="e => addressChange(e, 'areaOptions', 'city')"
                  >
                    <a-select-option v-for="item in cityOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="区"
                    v-model="queryParam.areaCode"
                    @change="e => addressChange(e, 'streetOptions', 'area')"
                  >
                    <a-select-option v-for="item in areaOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="街道"
                    v-model="queryParam.streetCode"
                    @change="e => addressChange(e, '', 'street')"
                  >
                    <a-select-option v-for="item in streetOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="商家"
                    v-model="queryParam.businessId"
                    @change="e => agentChange(e, 'networkOptions', 'agentCode')"
                  >
                    <a-select-option v-for="item in agentOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="网点"
                    v-model="queryParam.networkId"
                    @change="e => agentChange(e, 'deviceOptions', 'networkCode')"
                  >
                    <a-select-option v-for="item in networkOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select placeholder="设备" v-model="queryParam.deviceId">
                    <a-select-option v-for="item in deviceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
              </a-row>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="设备编号">
              <a-input placeholder="请输入" v-model="queryParam.deviceId"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="订单ID">
                <a-input placeholder="请输入" v-model="queryParam.orderId"></a-input>
              </a-form-item>
            </a-col>
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="开门类型">
                <j-dict-select-tag allowClear v-model="queryParam.openType" placeholder="请选择" dictCode="openType" />
              </a-form-item>
            </a-col>
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="创建时间">
                <a-range-picker allowClear class="per100" v-model="tempTimes" @change="timeChange" />
              </a-form-item>
            </a-col>
          </template>

          <a-col :sm="24" :md="12" :xl="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域 -->
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :scroll="{ x: 1200 }"
      @change="handleTableChange"
    >
      <template slot="avatarslot" slot-scope="text, record, index">
        <div class="anty-img-wrap">
          <a-avatar shape="square" :src="getAvatarView(record.avatar)" icon="user" />
        </div>
      </template>
      <span slot="action" slot-scope="text, record">
        <a-button
          type="primary"
          class="ml5 mr5"
          size="small"
          v-if="!record.inBlacklist"
          @click="
            data.userId = record.userId
            visible = true
          "
          >拉黑用户</a-button
        >
      </span>
    </a-table>

    <a-modal v-model="visible" title="拉黑原因" @ok="joinBlack">
      <a-textarea v-model="data.blackReason" placeholder="请输入拉黑原因" />
    </a-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterDictText } from '@/components/dict/JDictSelectUtil'
import { getFileAccessHttpUrl, postAction } from '@/api/manage'
import Api from '@/api/ServiceManage/ScanningRecord'
import BlackApi from '@/api/ServiceManage/BlackList'

export default {
  name: 'ScanningRecord',
  mixins: [JeecgListMixin],
  data() {
    return {
      url: {
        list: Api.list
      },
      columns: [],
      openTypeOptions: [],
      visible: false,
      data: {
        userId: '',
        blackReason: ''
      }
    }
  },
  created() {
    this.columns = [
      {
        title: '用户/微信昵称',
        dataIndex: 'wechatName',
        fixed: 'left',
        align: 'center',
        width: 150
      },
      {
        title: '用户头像',
        dataIndex: 'avatar',
        align: 'center',
        scopedSlots: { customRender: 'avatarslot' }
      },
      {
        title: '代理商',
        align: 'center',
        dataIndex: 'agentName'
      },
      {
        title: '网点',
        align: 'center',
        dataIndex: 'networkName'
      },
      {
        title: '设备编号',
        align: 'center',
        dataIndex: 'deviceId',
        sorter: (a, b) => {
          return a.deviceId - b.deviceId
        }
      },
      {
        title: '开门类型',
        dataIndex: 'openType',
        align: 'center',
        customRender: (text, record, index) => {
          //字典值替换通用方法
          return filterDictText(this.openTypeOptions, text)
        },
        sorter: (a, b) => {
          return a.openType - b.openType
        }
      },
      {
        title: '格子锁号',
        align: 'center',
        dataIndex: 'cellLock',
        sorter: (a, b) => {
          return a.cellLock - b.cellLock
        }
      },
      {
        title: '设备格子别名',
        align: 'center',
        dataIndex: 'cellName',
        sorter: (a, b) => {
          return a.cellName - b.cellName
        }
      },
      {
        title: '订单号',
        align: 'center',
        dataIndex: 'orderId',
        sorter: (a, b) => {
          return a.orderId - b.orderId
        }
      },
      {
        title: '创建时间',
        align: 'center',
        dataIndex: 'createTime',
        sorter: (a, b) => {
          return a.createTime - b.createTime
        }
      },
      {
        title: '操作',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      }
    ]

    this.getAddress('provinceOptions')
  },
  methods: {
    // 初始化数据字典
    initDictConfig() {
      //初始化字典 - 是否
      initDictOptions('openType').then(res => {
        if (res.success) {
          this.openTypeOptions = res.result
        }
      })
    },
    // 创建时间改变
    timeChange(e, arr) {
      if (arr[0]) {
        this.queryParam.startTime = arr[0]
        this.queryParam.endTime = arr[1]
      } else {
        this.queryParam.startTime = undefined
        this.queryParam.endTime = undefined
      }
    },

    getAvatarView: function(avatar) {
      return getFileAccessHttpUrl(avatar)
    },

    // 加入黑名单
    joinBlack() {
      postAction(BlackApi.add, this.data).then(res => {
        this.$message.success(res.message)
        this.visible = false
        this.loadData()
      })
    }
  }
}
</script>

<style scoped></style>

